<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/6/14
 * Time: 下午6:29
 */

namespace Common\Service;


use Miniproapi\Service\LogInfoService;
use Think\Exception;

class SmsService extends BaseService
{

    public $smsLogModel;
    public function __construct()
    {
        vendor('dayu.top');
        $this->smsLogModel=M('mallsmslog');

    }

    /*
  * 阿里大鱼短信
  * $sign 短信签名
  * $data 数组
  * $tmpid 模板id
  * $mobile手机号码, 多个用逗号隔开
  */
   public function alisms($mobile,$data,$sign="身份验证",$tmpid="SMS_6280751") {
        if(!$mobile){
            return false;
        }
        vendor('dayu/TopSdk');
        $c = new \TopClient;
        $c->appkey = C('ALIDAYU_APPKEY');
        $c->secretKey = C('ALIDAYU_APPSEC');
        $c->format = "json";
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("1237777");
        $req->setSmsType("normal");//短信类型，传入值请填写normal
        $req->setSmsFreeSignName($sign);//短信签名
        $json = json_encode($data);
        $req->setSmsParam($json);
        $req->setRecNum($mobile);
        $req->setSmsTemplateCode($tmpid);//
        //短信模板ID，传入的模板必须是在阿里大鱼“管理中心-短信模板管理”中的可用模板。示例：SMS_585014
        $resp = $c->execute($req);
        return $resp;

    }

    /**
     * 记录发送短信日志
     * @param $addInfo
     * @return bool|mixed
     */
    public function addLog($addInfo){
       try{
        return $this->smsLogModel->add($addInfo);
       }catch (Exception $ex){
           LogInfoService::S()->writeLog('DataBaseError','addSmsLogError',$ex->getMessage());
           return false;
       }
    }

}